var addressFillType = 'startAddress';
var addressSelectPopup;
function showSelectAddressDialog(type){
	addressFillType = type;
	
	addressSelectPopup = new Best.AddressSelectPopup(
			{
				rowdblclick: fillAddress
			});
				
}
function fillAddress(event){
	var addressBook = event.selModel.getSelected().data;
	if(addressFillType == 'startAddress'){
		var allValue = [];
		allValue.push(addressBook.province);
		allValue.push(addressBook.city);
		allValue.push(addressBook.town);
		shippingOrder_senderAddressProvincemultiselect.setAllValue(allValue);
		jQuery('#form1_shippingOrder_senderAddressStreet')[0].value = toEmptyIfNull(addressBook.street);
		jQuery('#form1_shippingOrder_senderName')[0].value = toEmptyIfNull(addressBook.name);
		jQuery('#form1_shippingOrder_senderTelephoneAreaCode')[0].value = toEmptyIfNull(addressBook.telephoneAreaCode);
		jQuery('#form1_shippingOrder_senderTelephoneNumber')[0].value = toEmptyIfNull(addressBook.telephoneNumber);
		jQuery('#form1_shippingOrder_senderTelephoneExtention')[0].value = toEmptyIfNull(addressBook.telephoneExtention);
		jQuery('#form1_shippingOrder_senderMobilePhone')[0].value = toEmptyIfNull(addressBook.mobilePhone);
		jQuery('#form1_shippingOrder_senderPostCode')[0].value = toEmptyIfNull(addressBook.postCode);
		
	}else{
		var allValue = [];
		allValue.push(addressBook.province);
		allValue.push(addressBook.city);
		allValue.push(addressBook.town);
		shippingOrder_recieverAddressProvincemultiselect.setAllValue(allValue);
		jQuery('#form1_shippingOrder_recieverAddressStreet')[0].value = toEmptyIfNull(addressBook.street);
		jQuery('#form1_shippingOrder_recieverName')[0].value = toEmptyIfNull(addressBook.name);
		jQuery('#form1_shippingOrder_recieverTelephoneAreaCode')[0].value = toEmptyIfNull(addressBook.telephoneAreaCode);
		jQuery('#form1_shippingOrder_recieverTelephoneNumber')[0].value = toEmptyIfNull(addressBook.telephoneNumber);
		jQuery('#form1_shippingOrder_recieverTelephoneExtention')[0].value = toEmptyIfNull(addressBook.telephoneExtention);
		jQuery('#form1_shippingOrder_recieverMobilePhone')[0].value = toEmptyIfNull(addressBook.mobilePhone);
		jQuery('#form1_shippingOrder_recieverPostCode')[0].value = toEmptyIfNull(addressBook.postCode);
	}
	addressSelectPopup.close();
}
function getAddressBook(id){
	if(addressBooks != null){
		var addressBook;
		for(var i=0; i<addressBooks.length; i++){
			addressBook = addressBooks[i];
			if(addressBook.id == id){
				return addressBook;
			}
		}
	}
	return null;
}
function computeFare(){
	var options = {
					url: 'computeFare.html',
					dataType: 'json',
					success: function(dataFromServer){
						var form1 = $('form1');
						validation.clearErrorMessages(form1);
						validation.clearErrorLabels(form1);
						if(dataFromServer[0] == 'success'){
							var tip = '试算费用如下(如有出入，请以实际发生费用为准)：\r\n';
							var shippingOrderFare, shippingOrderFares = dataFromServer[4].shippingOrderFares;
							for(var i=0; i<shippingOrderFares.length; i++){
								shippingOrderFare = shippingOrderFares[i];
								tip += '\t快递公司: ' + shippingOrderFare.logisticCompany + '\r\n\t费用(元）: ' + shippingOrderFare.fare + '\r\n\t时效: ' + shippingOrderFare.ageing + '\r\n\r\n';
							}
							alert(tip);
						}else{
							
							validation.addFieldErrors(dataFromServer[3], form1);
						}
					}
				};
	jQuery('#form1').ajaxSubmit(options); 
	return false;
}
function showFreightSelectPopup(){
	var freightSelectPopup = new Best.FreightSelectPopup({
		url: freightListUrl,
		isShowAvailableAmount: false,
		callback: function(records){
			var record;
			
			var freightDescInput = $('form1_shippingOrder_freightDesc');
			var weightInput = $('form1_shippingOrder_weight');
			for(var i=0; i<records.length; i++){
				record = records[i];
				var v = Utils.RTrim(freightDescInput.value);
				if(v == ''){
					weightInput.value = record.get('weight');
				}else{
					var oldWeight = parseInt(weightInput.value);
					if(!isNaN(oldWeight)){
						weightInput.value = oldWeight + record.get('weight');
					}else{
						weightInput.value = record.get('weight');
					}
				}
				if(v != ''){
					v = v.replace(/;* *$/g, '');
					v += '; ';
				}
				v += record.get('name');
				freightDescInput.value = v;
				
			}
		}
	});
}
/**
 * 当用户改变收发货地址的时候触发该函数去获取有能力运这票货的承运商
 */
function onAddressChange(){
	var options = {
					url: 'getCapableCarriers.html',
					dataType: 'json',
					success: function(dataFromServer){
						var form1 = $('form1');
						validation.clearErrorMessages(form1);
						validation.clearErrorLabels(form1);
						if(dataFromServer[0] == 'success'){
							showCapableCarriers(dataFromServer[4].carriers);
						}else{
							alert('获取快递公司列表失败!');
						}
					}
				};
	jQuery('#form1').ajaxSubmit(options); 
	return true;
}
/**
 * 显示承运商列表到快递公司下拉框中
 */
function showCapableCarriers(carriers){
	var carrierSelect = $('form1_shippingOrder_carrierCode');
	var options = carrierSelect.options;
	var carrier;
	var innerHTML = '';
	for(var i=0; i<carriers.length; i++){
		carrier = carriers[i];
		carrierSelect.options[i] = new Option(carrier.name, carrier.code); 
	}
}
function bindCarrierSelectToAddresses(){
	var oldOnChange = jQuery('#shippingOrder_senderAddressTown')[0].onchange;
	if(oldOnChange != null){
		jQuery('#shippingOrder_senderAddressTown')[0].onchange = function(){
			oldOnChange();
			onAddressChange();
		}
	}else{
		jQuery('#shippingOrder_senderAddressTown')[0].onchange = function(){
			onAddressChange();
		}
	}
	var oldOnChange = jQuery('#shippingOrder_recieverAddressTown')[0].onchange;
	if(oldOnChange != null){
		jQuery('#shippingOrder_recieverAddressTown')[0].onchange = function(){
			oldOnChange();
			onAddressChange();
		}
	}else{
		jQuery('#shippingOrder_recieverAddressTown')[0].onchange = function(){
			onAddressChange();
		}
	}
	
}